<template>
  <div class="card">
    <header class="card-header">
      <h2>in( ) SQL生成器</h2>
    </header>
    <main class="card-body">
      <textarea v-model="inputText" placeholder="请输入多行文本，每行一个值" rows="5" cols="50" class="input-textarea"></textarea>
      <button @click="generateSql" class="generate-button">
        生成 SQL 语句
      </button>
      <button v-if="generatedSql" @click="copyToClipboard" class="copy-button">
        复 制
      </button>
      <textarea ref="generatedSql" readonly class="output-textarea" :value="generatedSql" @mousedown="startResize"
        @mousemove="resize" @mouseup="endResize"></textarea>
    </main>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputText: "",
      generatedSql: "",
      sqlGenerated: false,
    };
  },
  methods: {
    generateSql() {
      // 将输入的文本按行分割
      const lines = this.inputText.split("\n").map((line) => line.trim());
      // 过滤掉空字符串
      const values = lines.filter((line) => line !== "");
      // 生成 SQL 语句
      this.generatedSql = `IN('${values.join("','")}')`;
      this.sqlGenerated = true;
    },
    copyToClipboard() {
      this.$refs.generatedSql.select();
      document.execCommand("copy");
      // alert("SQL代码已复制到剪贴板!");
      this.$toast('SQL代码已复制到剪贴板!', 1000, 50);
    },
    startResize(event) {
      this.resizing = true;
      this.textareaHeight = event.clientY;
      window.addEventListener("mouseup", this.endResize);
    },
    resize(event) {
      if (this.resizing) {
        const delta = event.clientY - this.textareaHeight;
        this.$refs.generatedSql.style.height = `${this.$refs.generatedSql.offsetHeight + delta
          }px`;
      }
    },
    endResize() {
      this.resizing = false;
      window.removeEventListener("mouseup", this.endResize);
    },
  },
};
</script>

<style scoped>
.card {
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  width: 600px;
}

.card-header {
  padding: 20px;
  background-color: #fff;
  border-bottom: 1px solid #ddd;
}

.card-body {
  padding: 20px;
}

.generate-button {
  margin-top: 10px;
  margin-bottom: 15px;
  padding: 10px 20px;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.copy-button {
  margin-top: 10px;
  margin-left: 20px;
  margin-bottom: 15px;
  padding: 10px 20px;
  background-color: #8d9fb3;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.output-textarea {
  width: 100%;
  height: 120px;
  resize: none;
  border: 1px solid #ddd;
  padding: 10px;
  box-sizing: border-box;
  line-height: 1.5;
}

label {
  display: block;
  margin-bottom: 10px;
}

input[type="number"] {
  width: 100px;
  padding: 5px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.input-textarea {
  width: 100%;
  height: 200px;
  border: 1px solid #ddd;
  padding: 10px;
  box-sizing: border-box;
  line-height: 1.5;
  resize: vertical;
}
</style>